package com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view;

import com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.struct.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author synhard
 * @description 研发流程tab对应的panel
 * @date 2024/5/6 15:45
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class DevProcessPanel extends JPanel {
    private JPanel contentPanel;
    private JButton runningJButton;
    private JButton runningHistoryJButton;
    private DevProcess devProcess;
    public RunningPanel runningPanel;
    public RunningHistoryPanel runningHistoryPanel;

    public DevProcessPanel(DevProcess devProcess) {
        this.devProcess = devProcess;
        initUI();
    }

    private void initUI() {
        setLayout(new BorderLayout());

        // 初始化按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        runningJButton = new JButton("运行流水线");
        this.runningJButton.setEnabled(false);
        runningHistoryJButton = new JButton("运行历史");

        runningJButton.addActionListener(e -> {
            runningPanel.getDataFetchTimer().start();
            updateContentPanel(runningPanel);
            runningJButton.setEnabled(false);
            runningHistoryJButton.setEnabled(true);
        });

        runningHistoryJButton.addActionListener(e -> {
            runningPanel.getDataFetchTimer().stop();
            runningHistoryPanel = new RunningHistoryPanel(devProcess.getPipeline().getPipelineId());
            updateContentPanel(runningHistoryPanel);
            runningJButton.setEnabled(true);
            runningHistoryJButton.setEnabled(false);
        });

        buttonPanel.add(runningJButton);
        buttonPanel.add(runningHistoryJButton);

        add(buttonPanel, BorderLayout.NORTH);

        // 初始化内容面板
        contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        add(contentPanel, BorderLayout.CENTER);

        // 初始化两个面板
        runningPanel = new RunningPanel(devProcess, this);
        runningHistoryPanel = new RunningHistoryPanel(devProcess.getPipeline().getPipelineId());

        // 默认显示第一个面板
        updateContentPanel(runningPanel);
    }

    public void updateContentPanel(JPanel newContent) {
        SwingUtilities.invokeLater(() -> {
            contentPanel.removeAll();
            contentPanel.add(newContent, BorderLayout.CENTER);
            contentPanel.revalidate();
            contentPanel.repaint();
        });
    }
}
